package longrise.phone.com.bjjt_henan.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.R;


/**
 * Created by Administrator on 2016/5/14.
 */
public class ApiImpI {

    private static ApiImpI instance;

    private String BASE_URL;

    private String KCKP_URL;

    private Handler mHandler;
    private Context mContext;

    private ApiImpI(Context context) {
        BASE_URL = context.getResources().getString(R.string.kckpServerUrl);
        KCKP_URL = context.getResources().getString(R.string.kckpServerUrl);
        mHandler = new Handler(Looper.getMainLooper());
        this.mContext = context;
    }

    public static ApiImpI getInstance(Context context) {
        if (instance == null) {
            synchronized (ApiImpI.class) {
                if (instance == null) {
                    instance = new ApiImpI(context);
                }
            }
        }
        return instance;
    }

    /**
     * 注册
     *
     * @param userflag:用户名
     * @param pwd:密码
     * @param phonenum:手机号码
     * @param code:验证码
     * @param longitude:经度
     * @param latitude:纬度
     * @param callBack：回调接口
     */
    public void register(String userflag, String pwd, String phonenum, String code,
                         String longitude, String latitude, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("password", pwd);
        bean.set("phonenumber", phonenum);
        bean.set("code", code);
        bean.set("caselon", longitude);
        bean.set("caselat", latitude);
        bean.set("clientmac", "6");
        run(BASE_URL, URLConstant.REGISTER, bean, callBack);
    }

    /**
     * 请求注册验证码
     *
     * @param phonenum:手机号码
     * @param imgCode:图片验证码
     * @param imgid:图片id
     * @param callBack：回调接口
     */
    public void getValidCode(String phonenum, String imgCode, String imgid, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("mobilenumber", phonenum);
        bean.set("imgcode", imgCode);
        bean.set("imgid", imgid);
        run(BASE_URL, URLConstant.VALIDCODE, bean, callBack);
    }


    /**
     * 请求图片验证码
     *
     * @param callBack：回调接口
     */
    public void picCodeCreate(RequestCallBack callBack) {
        run(BASE_URL, URLConstant.CODECREATE, null, callBack);
    }

    /**
     * 请求找回密码验证码
     *
     * @param phonenum:手机号码
     * @param callBack：回调接口
     */
    public void getPwdCode(String phonenum, String imgCode, String imgid, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("mobilenumber", phonenum);
        bean.set("imgcode", imgCode);
        bean.set("imgid", imgid);
        run(BASE_URL, URLConstant.PWDCODE, bean, callBack);
    }

    /**
     * 请求修改手机号码验证码
     *
     * @param phonenum:手机号码
     * @param callBack：回调接口
     */
    public void getPhoneCode(String phonenum, String imgCode, String imgid, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("mobilenumber", phonenum);
        bean.set("imgcode", imgCode);
        bean.set("imgid", imgid);
        run(BASE_URL, URLConstant.PHONECODE, bean, callBack);
    }

    /**
     * 登录
     *
     * @param userflag:用户名
     * @param pwd:密码
     * @param callBack：回调接口
     */
    public void login(String userflag, String pwd, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("password", pwd);
        run(BASE_URL, URLConstant.LOGIN, bean, callBack);
    }

    /**
     * 找回密码
     *
     * @param flag:找回方式：表示手机找回
     * @param phonenum:手机号码
     * @param code:验证码
     * @param newPwd:新密码
     * @param callBack：回调接口
     */
    public void forgetPwd(String flag, String phonenum, String code,
                          String newPwd, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
//        bean.set("flag", flag);
        bean.set("phonenumber", phonenum);
        bean.set("code", code);
        bean.set("userpwd", newPwd);
        run(BASE_URL, URLConstant.FORGETPWD, bean, callBack);
    }

    /**
     * 修改密码
     *
     * @param userflag:登录名
     * @param oldPwd:旧密码
     * @param token:验证字符串
     * @param newPwd:新密码
     * @param callBack：回调接口
     */
    public void modifyPwd(String userflag, String oldPwd, String newPwd,
                          String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("passwordold", oldPwd);
        bean.set("passwordnew", newPwd);
        bean.set("token", token);
        run(BASE_URL, URLConstant.MODIFYPWD, bean, callBack);
    }

    /**
     * 查询未接收的推送消息
     *
     * @param appcaseno:移动报案号
     * @param msgtype:消息类型
     * @param token:验证字符串
     * @param username:用户名
     * @param callBack：回调接口
     */
    public void searchUnMsg(String appcaseno, String msgtype, String username,
                            String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("appcaseno", appcaseno);
        bean.set("msgtype", msgtype);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.SEARCH_UNRECEIVEMSG, bean, callBack);
    }

    /**
     * 实时接收到消息后调用
     *
     * @param id:案件id
     * @param msgtype:消息类型
     * @param token:验证字符串
     * @param username:新密码
     * @param callBack：回调接口
     */
    public void receiveCallBack(String id, String msgtype, String username,
                                String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("id", id);
        bean.set("msgtype", msgtype);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.RECEIVEMSG_CALLBACK, bean, callBack);
    }

    /**
     * 修改联系电话
     *
     * @param userflag:用户名
     * @param oldPhonenum:旧手机号
     * @param newPhonenum:手机号码
     * @param code:验证码
     * @param token:验证字符串
     * @param pwd:密码
     * @param callBack：回调接口
     */
    public void modifyPhone(String userflag, String oldPhonenum, String newPhonenum,
                            String code, String token, String pwd, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("mobilephoneold", oldPhonenum);
        bean.set("mobilephone", newPhonenum);
        bean.set("code", code);
        bean.set("token", token);
        bean.set("password", pwd);
        run(BASE_URL, URLConstant.MODIFYPHONE, bean, callBack);
    }

    /**
     * 绑定机动车
     *
     * @param cartype:车型
     * @param carno:车牌
     * @param frameno:车架号
     * @param drivingno:行驶证照片
     * @param userflag:密码
     * @param token:验证字符串
     * @param callBack：回调接口
     */
    public void addAcccarhen(String cartype, String carno, String frameno,
                             String drivingno, String userflag, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("cartype", cartype);
        bean.set("casecarno", carno);
        bean.set("frameno", frameno);
        bean.set("drivingnoimg", drivingno);
        bean.set("userflag", userflag);
        bean.set("token", token);
        run(BASE_URL, URLConstant.ADD_ACCCARHEN, bean, callBack);
    }

    /**
     * 绑定驾驶证
     *
     * @param userflag:用户名
     * @param token:验证字符串
     * @param username:姓名
     * @param cardno:身份证号
     * @param fileno:档案编号
     * @param drivingno:驾驶证照片
     * @param callBack：回调接口
     */
    public void addDriverInfo(String userflag, String token, String username, String cardno,
                              String fileno, String drivingno, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("cardno", cardno);
        bean.set("driverfileno", fileno);
        bean.set("token", token);
        bean.set("username", username);
        bean.set("drivernoimg", drivingno);
        run(BASE_URL, URLConstant.ADD_DRIVERINFO, bean, callBack);
    }

    /**
     * 修改车辆
     *
     * @param id:该条数据的id
     * @param carno:车牌
     * @param frameno:车架号
     * @param drivingno:行驶证照片
     * @param userflag:登录名
     * @param cartype:车辆类型
     * @param token:验证字符串
     * @param callBack：回调接口
     */
    public void modifyCar(String id, String userflag, String carno, String frameno,
                          String cartype, String token,
                          String drivingno, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("id", id);
        bean.set("userflag", userflag);
        bean.set("casecarno", carno);
        bean.set("frameno", frameno);
        bean.set("cartype", cartype);
        bean.set("token", token);
        bean.set("drivingnoimg", drivingno);
        run(BASE_URL, URLConstant.MODIFYCAR, bean, callBack);
    }

    /**
     * 删除车辆信息
     *
     * @param id:车辆id
     * @param userflag:登录名
     * @param token:验证字符串
     * @param callBack：回调接口
     */
    public void deleteCar(String id, String userflag, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("id", id);
        bean.set("userflag", userflag);
        bean.set("token", token);
        run(BASE_URL, URLConstant.DELETECAR, bean, callBack);
    }

    /**
     * 修改驾驶证
     *
     * @param id:该条数据的id
     * @param cardno:身份证号
     * @param username:姓名
     * @param drivingno:行驶证照片
     * @param userflag:登录名
     * @param driverfileno:档案编号
     * @param token:验证字符串
     * @param callBack：回调接口
     */
    public void modifyDriverinfo(String id, String userflag, String cardno, String username,
                                 String driverfileno, String token,
                                 String drivingno, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("id", id);
        bean.set("cardno", cardno);
        bean.set("userflag", userflag);
        bean.set("token", token);
        bean.set("username", username);
        bean.set("driverfileno", driverfileno);
        bean.set("drivernoimg", drivingno);
        run(BASE_URL, URLConstant.MODIFY_DRIVERINFO, bean, callBack);
    }

    /**
     * 查看用户信息
     *
     * @param username：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void searchPersonInfo(String username, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", username);
        bean.set("token", token);
        run(BASE_URL, URLConstant.SEARCH_PERSONINFO, bean, callBack);
    }

    /**
     * 是否绑定信息
     *
     * @param id：用户id
     * @param username：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void bindState(String id, String username, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userid", id);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.QUERYBINDINGCARS, bean, callBack);
    }

    /**
     * 查询驾驶证信息
     *
     * @param ownername：车主姓名
     * @param username：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void getCarOwner(String ownername, String username, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("carownername", ownername);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.KCKPAPPGETCAROWNER, bean, callBack);
    }

    /**
     * 查询用户绑定的车辆信息
     *
     * @param id：车主姓名
     * @param username：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void searchCars(String id, String username, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userid", id);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.KCKPSEARCHCARS, bean, callBack);
    }

    /**
     * 历史案件
     *
     * @param userId：车牌号
     * @param phone：手机号
     * @param type：案件类型
     * @param username：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void searchAllCase(String userId, String phone, String type, String username,
                              String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userid", userId);
        bean.set("telephone", phone);
        bean.set("querytype", type);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.ALLCASES, bean, callBack);
    }

    /**
     * 事故详情信息接口
     *
     * @param appcaseno：移动报案号
     * @param phone：手机号
     * @param username：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void accidentDetails(String appcaseno, String phone, String username,
                                String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("casenumber", appcaseno);
        bean.set("appphone", phone);
        bean.set("username", username);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.ACCIDENT_DETAILS, bean, callBack);
    }

    /**
     * 获取区域id
     *
     * @param longitude：经度
     * @param latitude：纬度
     * @param userflag：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void getAreaId(String longitude, String latitude, String userflag,
                          String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("maplon", longitude);
        bean.set("maplat", latitude);
        bean.set("userflag", userflag);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.CHECKAREAID, bean, callBack);
    }

    /**
     * 检测时间
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void checkTime(String userflag, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.CHECKTIME, bean, callBack);
    }

    /**
     * 不保险报案，放弃理赔
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void giveupCps(String appno, String userflag, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("appcaseno", appno);
        bean.set("userflag", userflag);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.GIVEUP_CPS, bean, callBack);
    }

    /**
     * 反馈
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param name：姓名
     * @param appphone：手机号
     * @param content：反馈意见
     * @param callBack：回调接口
     */
    public void feedback(String userflag, String token, String name, String appphone, String content, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("token", token);
        bean.set("name", name);
        bean.set("appphone", appphone);
        bean.set("suggestion", content);
        run(KCKP_URL, URLConstant.FEED_BACK, bean, callBack);
    }

    /**
     * 首页新闻查询
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param type：news类型
     * @param callBack：回调接口
     */
    public void searchNews(String userflag, String token, String type, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("token", token);
        bean.set("newstype", type);
        run(KCKP_URL, URLConstant.SEARCH_NEWS, bean, callBack);
    }

    /**
     * 收藏(取消收藏)新闻
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param id：新闻id
     * @param callBack：回调接口
     */
    public void collectNews(String userflag, String token, String id, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("token", token);
        bean.set("newsid", id);
        run(KCKP_URL, URLConstant.COLLECT_NEWS, bean, callBack);
    }

    /**
     * 我的收藏列表
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param nextid：新闻id
     * @param pagesize：数目
     * @param callBack：回调接口
     */
    public void collectNewsList(String userflag, String token, String nextid, String pagesize, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("token", token);
        bean.set("nextid", nextid);
        bean.set("pagesize", pagesize);
        run(KCKP_URL, URLConstant.NEWS_LIST, bean, callBack);
    }

    /**
     * 是否收藏该新闻
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param id：新闻id
     * @param callBack：回调接口
     */
    public void checkCollect(String userflag, String token, String id, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", userflag);
        bean.set("token", token);
        bean.set("newsid", id);
        run(KCKP_URL, URLConstant.CHECK_COLSTATE, bean, callBack);
    }

    /**
     * 查询该区域下所有产险公司列表
     *
     * @param userflag：用户名
     * @param token：验证字符串
     * @param callBack：回调接口
     */
    public void searchCplist(String userflag, String token, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("username", userflag);
        bean.set("token", token);
        run(KCKP_URL, URLConstant.SEARCH_CPLIST, bean, callBack);
    }

    /**
     * 执行网络请求
     */
    private void run(String url, String serviceName, EntityBean bean, final RequestCallBack callBack) {
        LoadDataManagerCar.getInstance(mContext).callService(null, url, serviceName, bean,
                new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, final Object result) {
                        StringBuffer success = new StringBuffer();
                        success.append("onSuccess--->");
                        success.append(((EntityBean) result).toString());
                        Log.i(service, success.toString());
                        if (null != callBack) {
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callBack.onSuccess(result);
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(String key, String service, final LoadDataManagerFather.ResultType resultType) {
                        StringBuffer error = new StringBuffer();
                        error.append("onError--->");
                        error.append(resultType.toString());
                        Log.i(service, error.toString());
                        if (null != callBack) {
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resultType == LoadDataManagerFather.ResultType.Failure) {
                                        callBack.onError("网络服务故障，请稍后重试");
                                    } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                                        callBack.onError("无网络连接，请检查您的网络");
                                    } else {
                                        callBack.onError(null);
                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }
                });
    }

    /**
     * 网络请求的回调接口
     */
    public interface RequestCallBack {

        void onError(String error);

        void onSuccess(Object result);
    }

    /**
     * 定责定损
     *
     * @param phonenum:手机号码
     * @param callBack：回调接口
     */
    public void getAgreementCode(String phonenum, String codeType, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("mobilephone", phonenum);
        bean.set("contenttype", codeType);
        run(BASE_URL, URLConstant.KCKPAPPSENDVERCODE, bean, callBack);
    }

    /**
     * 驾驶证和行驶证图像识别
     *
     * @param photo    图像的String
     * @param type     2-二代身份证正面，3-二代身份证反面 5-驾驶证 6-行驶证
     * @param callBack
     */
    public void picIdentification(String photo, String type, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("photo", photo);
        bean.set("type", type);
        run(BASE_URL, URLConstant.LCIPAPPIMGDATA, bean, callBack);
    }

    /**
     * 驾驶证和行驶证图像识别
     *
     * @param photo    图像的String
     * @param type     2-二代身份证正面，3-二代身份证反面 5-驾驶证 6-行驶证
     * @param callBack
     */
    public void getOcrInfo(String photo, String type, RequestCallBack callBack) {
        EntityBean bean = new EntityBean();
        bean.set("image", photo);
        bean.set("username", "HNKCKP");
        bean.set("userkey", "henankckp");
        bean.set("type", type);
        run(BASE_URL, URLConstant.OCR_INFO, bean, callBack);
    }

}